package com.five.controller.backstage;

import com.alibaba.fastjson.JSON;
import com.five.dao.backstage.StaffPwdDao;
import com.five.dao.impl.backstage.StaffPwdDaoImpl;
import com.five.entity.backstage.TStaffPwd;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/login/*")
public class StaffPwdController extends HttpServlet {
    StaffPwdDao pd = new StaffPwdDaoImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求数据的编码方式
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String state = req.getParameter("state");
        Writer out = resp.getWriter();
        if (null != state) {
            switch (state) {
                case "find":find(req, resp, out);break;
                case "loginOut":loginOut(req, resp, out);break;
                case "update":update(req, resp, out);break;
                default:break;
            }
        }
        out.flush();
        out.close();
    }

    //修改密码
    private void update(HttpServletRequest req, HttpServletResponse resp, Writer out) throws IOException {
        HttpSession session = req.getSession();
        String name = session.getAttribute("name")+"";
        String newPwd = req.getParameter("password");
        String oldPwd = req.getParameter("oldPassword");
        int result = pd.update(oldPwd, newPwd, name);
        json(out,result);
    }

    //退出
    private void loginOut(HttpServletRequest req, HttpServletResponse resp, Writer out) throws IOException, ServletException {
        HttpSession session = req.getSession();
        session.removeAttribute("name");
        session.removeAttribute("pwd");
        session.removeAttribute("imgpath");
        session.removeAttribute("id");
        session.removeAttribute("roleid");
//        session.invalidate();//session失效
        //登录页面
       resp.sendRedirect("/Hotel/view/backstage/backstage.jsp");
    }

    /**
     *登录
     */
    private void find(HttpServletRequest req, HttpServletResponse resp, Writer out) throws IOException {
        //取出前端的姓名 密码
        TStaffPwd s = new TStaffPwd();
        s.setName(req.getParameter("loginUsername"));
        s.setPwd(req.getParameter("loginPassword"));
        HttpSession session = req.getSession();
        // result=1成功  result=0失败
        int result = pd. find(s);
        if(result==1){
           session.setAttribute("name",s.getName());
           session.setAttribute("pwd",s.getPwd());
           session.setAttribute("imgpath",pd.path(s.getName()));
           session.setAttribute("id",pd.ID(s.getName()));
           session.setAttribute("roleid",pd.roleid(s.getName()));
        }
        json(out,result);
    }

    private void json(Writer out,Object result) throws IOException {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count",0);
        map.put("data", result);
        String s = JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
        out.write(s);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
